<?php

class RegistercompanyController extends MemberBaseController
{
	public $layout ="//layouts/member";
	
	public function actionRegister()
	{

		$model = CompanyCard::model()->findByPk(Yii::app()->user->getId());
		$company = Company::model()->findByPk(Yii::app()->user->getId());
		if(!isset($_GET['step'])){
			$this->render('notice',array(
					'model'=>$model,
					'company'=>$company,
					));
			exit;
		}
		
		//上传个人企业资料，在线申请
		if($_GET['step']==1){
			if(empty($model)){
				$model = new CompanyCard();
				$model->zm_id = Yii::app()->user->getId();
				
				if(isset($_POST['CompanyCard'])){
					$model->attributes = $_POST['CompanyCard'];
					if($model->save())
						$this->redirect(array('register','step'=>'1'));
				}
					
				$this->render('step_one',array('model'=>$model));
				exit;
			}
			
			//资料提交成功，请耐心等待照片审核
			if($model->zoa_check==1){
				$this->render('step_one_success',array('model'=>$model));
				exit;
			}
			
			//照片审核不通过,可选择重新填写
			if($model->zoa_check==3){
				if(isset($_GET['do'])&&$_GET['do']=='reset'){
					if(isset($_POST['CompanyCard'])){
						$model->attributes = $_POST['CompanyCard'];
						$model->zoa_check = 1;//设审核状态不待审核
						$model->zoa_onreason = '';//清空不通过的理由
						$model->inputtime = time();//更新申请时间
						if($model->save())
							$this->redirect(array('register','step'=>'1'));
					}
					$this->render('step_one',array('model'=>$model));
					exit;
				}
				
				$this->render('step_one_nopass',array(
						'model'=>$model,
				));
				exit;
			}
		}

		//签订服务协议
		if($_GET['step']==2){
			if(isset($_POST['CompanyCard'])){
				$model->attributes = $_POST['CompanyCard'];
				$model->zoa_accept_time = time();//协议认可时间为当前时间
				if($model->save())
					$this->redirect(array('register'));
			}
			$this->render('step_tow',array('model'=>$model));
			exit;

		}
		
		//缴纳保障金
		if($_GET['step']==3){
			if(isset($_GET['do'])&&$_GET['do']=='pay'){
						$request['out_trade_no'] = "153545";
						$request['subject'] = '商家服务保障金';
						$request['body'] = '商家服务保障金';
						$request['price'] = $model->category->zca_deposit;

						Payment::model()->pay($request,4);
						exit();
			}
			$this->render('step_three',array('model'=>$model));
			exit;
		}
		
		//添写商家资料
		if($_GET['step']==4){
			$company = new Company();
			$company->zca_id = $model->zca_id;
			if(isset($_POST['Company'])){
				$company->attributes = $_POST['Company'];
				$company->zct_id = F::setting('default_company_template');//默认商家模板
				$company->zc_deposit = $model->zoa_deposti;//保障金
				$company->zc_deposit_status = 2;//保证金审核状态：1.未审核，2审核通过
				$company->zc_status = 2;//1未审2已审3锁定',
				if($company->save()){
					//会员等级记录
				    $memglog = new	MemGlog();
				    $memglog->userid = $company->zm_id;
				    $memglog->itemname = F::setting('default_company_group');//默认商家等级
				    $memglog->save();
					$this->redirect(array('register'));
					exit();
				}
			}
			$this->render('step_four',array('model'=>$company));
			exit;
		}

	}
	
	public function actionAwait()
	{
		$this->render('await');
	}

}